import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import * as contractManagementApi from '../api/ContractManagementApi';
import LanguageConstant from '../constant/LanguageConstant';
import _ from 'lodash';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';
import * as Constant from '../constant/Constant';

const confirm = Modal.confirm
const {TableStore} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

class WorkflowSettingListStore {


    /**
     * 权限控制
     */
    @observable authorized = true;

    /**
     * 加载状态
     *
     * @type {boolean}
     */
    @observable loading = false;

    /**
     * 流程树
     */
    @observable workflowTreeDatas = [];

    /**
     * 选择的流列表的id
     *
     * @type {Array}
     */
    @observable selectedWorkflowKeys = [];

    /**
     * 流程树展开的key
     *
     * @type {Array}
     */
    @observable workflowTreeExpandedKeys = [];

    /**
     * 流程树渲染
     *
     * @type {number}
     */
    @observable workflowTreeRerender = 1;

    /**
     * 设置流程列表的table store
     */
    @observable settingWorkflowListTableStore = new TableStore();

    /**
     * 布局右侧显示的内容
     * workflowTable:显示列表
     * settingDetail：设置详情
     * @type {string}
     */
    @observable layoutRightType = Constant.WORKFLOW_LIST_SETTING_RIGHTLAYOUT_TABLE;

    /**
     * 选择的流程的id
     *
     * @type {number}
     */
    @observable selectedWorkflowId = -1;

    /**
     * 流程设置详情的tab页的key
     *
     * @type {string}
     */
    @observable workflowSettingTabKey = Constant.WORKFLOW_SETTING_TAB_BASEINFO;

    /**
     * 开启分权管理
     */
    @observable detachedMaintenance;

    /**
     * 选择的分部
     */
    @observable selectedSubCompanyId = -1;

    /**
     * 流程设置详情tab页签变化事件
     *
     * @param tabKey
     */
    @action changeWorkflowSettingTabKey = (tabKey) => {
        this.workflowSettingTabKey = tabKey;
    }

    /**
     * 重置流程列表相关
     */
    @action
    resetWorkflowSettingList = () => {
        this.layoutRightType = Constant.WORKFLOW_LIST_SETTING_RIGHTLAYOUT_TABLE;
        this.selectedWorkflowId = -1;
    }

    /**
     * 分部树选择对应数据
     *
     * @param params
     */
    onSelectBySubcompany = (params = {}) => {
        const {subCompanyId} = params;
        this.selectedSubCompanyId = subCompanyId;
        this.layoutRightType = Constant.WORKFLOW_LIST_SETTING_RIGHTLAYOUT_TABLE;
        this.selectedWorkflowId = -1;
        const queryDatas = {subCompanyId: subCompanyId};
        this.getWorkflowListTableData(queryDatas);
    }

    /**
     * 加载系统设置
     */
    @action
    getSystemSetting = () => {
        contractManagementApi.getSystemSetting().then(res => {
            const {code, data} = res;
            if (code == 200) {
                const {detachedMaintenance} = data;
                this.detachedMaintenance = detachedMaintenance;
            }
        });
    }


    /**
     * 获取流程列表sessionKey
     *
     */
    @action
    getWorkflowListTableData = (params) => {
        this.loading = true;
        contractManagementApi.getWorkflowList(params).then(res => {
            this.loading = false;
            const {code, data} = res;
            if (code == 401) {
                this.authorized = false;
            }
            if (code == 200) {
                this.settingWorkflowListTableStore.getDatas(data.sessionkey);
            }
        });
    }

    @action
    onSelectWorkflowListRows = (selectedKeys, selectedInfo) => {
        console.log("onSelectWorkflowListRows ", selectedKeys, toJS(selectedKeys));
        this.selectedWorkflowKeys = selectedKeys;
    }

    /**
     * 流程设置列表右侧操作按钮
     *
     * @param record
     * @param flag
     */
    @action
    onWorkflowListOperatesClick = (record, flag) => {
        flag == 0 && this.openSettingDetail(record.wfid);
        flag == 1 && this.affirmDeleteWorkflowSetting(record.id, false);
    }

    /**
     * 删除流程设置
     *
     * @param ids
     * @param multiDelete
     */
    @action
    affirmDeleteWorkflowSetting = (ids, multiDelete) => {
        if (multiDelete) {
            if (this.selectedWorkflowKeys.length <= 0) {
                message.error(getLabel("518571", "请先选择待删除数据"));
                return;
            }
            ids = this.selectedWorkflowKeys.join(",");
        }
        confirm({
            title: getLabel('518566', "信息确认"),
            content: getLabel('518567', "确定要删除吗？"),
            okText: getLabel('518568', "是"),
            cancelText: getLabel('518569', "否"),
            onOk: () => this.doDeleteWorkflowSetting({ids}),
            onCancel() {
            },
        });
    }


    /**
     * 删除流程设置
     *
     * @param params
     */
    @action
    doDeleteWorkflowSetting = (params) => {
        contractManagementApi.deleteWorkflowSetting(params).then(res => {
                const {code} = res;
                this.selectedWorkflowKeys = [];
                if (code == 200) {
                    message.success(getLabel("518560", "操作成功"));
                    this.refreshWorkflowListTable();
                } else {
                    message.error(getLabel("518561", "操作失败"));
                }
            }
        );
    }

    /**
     * 刷新流程列表数据
     *
     * @param params
     */
    @action
    refreshWorkflowListTable = (params) => {
        this.resetWorkflowSettingList();
        this.getWorkflowTree();
        this.getWorkflowListTableData();
    }

    /**
     * 加载流程路径树
     *
     * @param params
     */
    @action
    getWorkflowTree = (params) => {
        contractManagementApi.getWorkflowTree(params).then(res => {
            const {code, data} = res;
            if (code == 401) {
                this.authorized = false;
            }
            if (code == 200) {
                this.workflowTreeDatas = data;
                this.workflowTreeRerender = Math.random();
            }
        });
    }

    /**
     * 流程树展开事件
     *
     * @param keys
     * @param node
     */
    @action
    onWorkflowTreeExpand = (keys, node) => {
        this.workflowTreeExpandedKeys = keys;
    }

    /**
     * 流程树选择事件
     *
     * @param key
     * @param count
     * @param countType
     */
    @action
    onWorkflowTreeSelect = (key, count, countType) => {
        const workflowTreeExpandedKeys = toJS(this.workflowTreeExpandedKeys);
        let indexOfKey = workflowTreeExpandedKeys.indexOf(key);
        if (indexOfKey > -1) {
            workflowTreeExpandedKeys.splice(indexOfKey, 1);
        } else {
            workflowTreeExpandedKeys.push(key);
        }
        this.workflowTreeExpandedKeys = workflowTreeExpandedKeys;
        let type = "" + key;
        // 选择流程类型
        if (type.indexOf("type_") == 0) {
            const workflowType = type.split("_")[1];
            const params = {type: workflowType};
            this.layoutRightType = Constant.WORKFLOW_LIST_SETTING_RIGHTLAYOUT_TABLE;
            this.selectedWorkflowId = -1;
            this.getWorkflowListTableData(params);
        } else if (type.indexOf("wf_") == 0) {
            const workflowId = parseIntValue(type.split("_")[1]);
            if (workflowId > 0) {
                this.openSettingDetail(workflowId);
            }
        }
    }

    /**
     * 打开设置详情页
     * @param workflowId
     */
    @action
    openSettingDetail = (workflowId) => {
        this.layoutRightType = Constant.WORKFLOW_LIST_SETTING_RIGHTLAYOUT_DETAIL;
        this.selectedWorkflowId = workflowId;
        this.workflowSettingTabKey = Constant.WORKFLOW_SETTING_TAB_BASEINFO;
    }

}

export default WorkflowSettingListStore;